package com.innext.kjqb.dialog;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.innext.kjqb.R;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 首页费用详情弹窗
 */

public class CostDetailsDialog extends DialogFragment {
    public static final String TAG = "CostDetails";
    @BindView(R.id.rl_cost_details)
    RecyclerView mRlCostDetails;
    @BindView(R.id.tv_checkMoney)
    TextView mCheckMoneyText;
    @BindView(R.id.tv_accountMoney)
    TextView mAccountMoneyText;
    @BindView(R.id.tv_total_profit)
    TextView mProfitText;
    @BindView(R.id.tv_totalMoney)
    TextView mTotalMoneyText;


    public static CostDetailsDialog newInstance(double checkMoney, double accountMoney, double profit) {
        CostDetailsDialog dialog = new CostDetailsDialog();
        Bundle bundle = new Bundle();
        bundle.putDouble("checkMoney", checkMoney);
        bundle.putDouble("accountMoney", accountMoney);
        bundle.putDouble("profit", profit);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_cost_details, container, false);
        ButterKnife.bind(this, view);
        setData();
        return view;
    }

    private void initDialog() {
        getDialog().getWindow().setGravity(Gravity.CENTER);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout((int) (dm.widthPixels * 0.9), getDialog().getWindow().getAttributes().height);
    }

    private void setData() {
        mCheckMoneyText.setText(new DecimalFormat("0.00").format(getArguments().getDouble("checkMoney")) + "元");
        mAccountMoneyText.setText(new DecimalFormat("0.00").format(getArguments().getDouble("accountMoney")) + "元");
        mProfitText.setText(new DecimalFormat("0.00").format(getArguments().getDouble("profit")) + "元");
        mTotalMoneyText.setText(new DecimalFormat("0.00").format(getArguments().getDouble("checkMoney") + getArguments().getDouble("accountMoney")
                + getArguments().getDouble("profit")) + "元");
        mRlCostDetails.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    public void onStart() {
        super.onStart();
        initDialog();
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        //修改commit方法为commitAllowingStateLoss
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getDialog() != null) {
            setShowsDialog(false);
        }
    }


    @OnClick({R.id.rl_cost_details, R.id.tv_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_cost_details:
                break;
            case R.id.tv_submit:
                dismiss();
                break;
        }
    }

}
